#include "martist.h"

MArtist::MArtist(QObject *parent)
    : QAbstractTableModel(parent)
{
    setObjectName("ModelArtist");
}

MArtist::~MArtist()
{
}

Qt::ItemFlags MArtist::flags(const QModelIndex& index) const
{
    Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
    if (index.isValid())
    {
        if(index.column() == DtArtist::Name)
            theFlags |= Qt::ItemIsSelectable;
    }
    return theFlags;
}

QVariant MArtist::data(const QModelIndex& index, int role) const
{
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= mArtist.size() ||
      index.column() < 0 || index.column() >= DtArtist::NbAttr)
    return QVariant();
  const DtArtist *artist = mArtist.at(index.row());
  if(role == Qt::DisplayRole || role == Qt::EditRole)
  {
      switch(index.column())
      {
      case DtArtist::Play:
          return artist->getPlay();
      case DtArtist::Add:
          return artist->getAdd();
      case DtArtist::Name:
          return artist->getName();
      case DtArtist::Heart:
          return artist->getHeartDelegate();
      default: ;
      }
  }
  if(role == Qt::ToolTipRole)
      return artist->getToolTip();

  return QVariant();
}

QVariant MArtist::headerData(int section, Qt::Orientation orientation,
                             int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if (orientation == Qt::Horizontal && section < DtArtist::NbAttr)
    {
        if(section == DtArtist::Name)
            return QObject::tr("Name");
    }
    return "";
}

int MArtist::rowCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : mArtist.size();
}

int MArtist::columnCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : DtArtist::NbAttr;
}

bool MArtist::insertRows(int , int , const QModelIndex&)
{
    return false;
}

bool MArtist::removeRows(int row, int count, const QModelIndex&)
{
  beginRemoveRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    mArtist.removeAt(row);
  endRemoveRows();
  return true;
}

QList<QHeaderView::ResizeMode> MArtist::getHorizontalResizeModes() const
{
    QList<QHeaderView::ResizeMode> t;
    for(int i = 0; i < DtArtist::NbAttr; i++)
        t.append(QHeaderView::Stretch);
  return t;
}

void MArtist::setList(QList<DtArtist *> &lArtist)
{
    mArtist = lArtist;
    reset();
}

QList<DtArtist *> MArtist::getList() const
{   
    return mArtist;
}
